package com.xmler.plugin.adbrocket.adb.command

import com.android.ddmlib.IDevice
import com.intellij.openapi.project.Project
import com.xmler.plugin.adbrocket.adb.command.receiver.DisplayReceiver
import com.xmler.plugin.adbrocket.utils.AdbUtil
import com.xmler.plugin.adbrocket.utils.Logger
import org.jetbrains.android.facet.AndroidFacet
import java.util.concurrent.TimeUnit
import javax.swing.JTextArea

class ClearDataCmd: ICommand {
    override fun run(
        project: Project,
        device: IDevice,
        facet: AndroidFacet,
        packageName: String,
        display: JTextArea
    ): Boolean {
        try {
            if (AdbUtil.isAppInstalld(device, packageName)) {
                device.executeShellCommand("pm clear $packageName", DisplayReceiver(display), 10L, TimeUnit.SECONDS)
                Logger.d("%s cleared data for app on %s", packageName, device.name)
                return true
            } else {
                Logger.e("%s is not installed on %s", packageName, device.name)
            }
        } catch (e: Exception) {
            Logger.e("Clear data failed... ${e.message}")
        }
        return false
    }
}